Python/Python Mcq Set 20 Sample Test,Sample questions

Question:
 What will be the output of the following Python code?

>>> class A:
	pass
>>> class B(A):
	pass
>>> obj=B()
>>> isinstance(obj,A)

1.True

2.False

3.Wrong syntax for isinstance() method

4.Invalid method for classes

Posted Date:-2022-01-02 12:02:00


Question:
 What will be the output of the following Python code?

class A:
    def __init__(self, x= 1):
        self.x = x
class der(A):
    def __init__(self,y = 2):
        super().__init__()
        self.y = y
def main():
    obj = der()
    print(obj.x, obj.y)
main()

1. Error, the syntax of the invoking method is wrong

2. The program runs fine but nothing is printed

3. 1 0

4.1 2

Posted Date:-2022-01-02 11:56:27


Question:
 What will be the output of the following Python code?

class Demo:
     def __init__(self):
         self.a = 1
         self.__b = 1
     def get(self):
         return self.__b
obj = Demo()
obj.a=45
print(obj.a)

1.The program runs properly and prints 45

2.The program has an error because the value of members of a class can’t be changed from outside the class

3. The program runs properly and prints 1

4.The program has an error because the value of members outside a class can only be changed as self.a=45

Posted Date:-2022-01-02 12:13:19


Question:
 Which of the following statements is true?

1. The __new__() method automatically invokes the __init__ method

2.The __init__ method is defined in the object class

3. The __eq(other) method is defined in the object class

4.The __repr__() method is defined in the object class

Posted Date:-2022-01-02 12:02:33


Question:
 Which of these is a private data field?

def Demo:
def __init__(self):
    __a = 1
    self.__b = 1
    self.__c__ = 1
    __d__= 1

1. __a

2.__b

3.__c__

4.__d__

Posted Date:-2022-01-02 12:12:19


Question:
Can one block of except statements handle multiple exception?

1.yes, like except TypeError, SyntaxError [,…]

2. yes, like except [TypeError, SyntaxError]

3.no

4.none of the mentioned

Posted Date:-2022-01-02 12:18:56


Question:
hat will be the output of the following Python code?

class A:
    def __init__(self):
        self.multiply(15)
        print(self.i)
 
    def multiply(self, i):
        self.i = 4 * i;
class B(A):
    def __init__(self):
        super().__init__()
 
    def multiply(self, i):
        self.i = 2 * i;
obj = B()

1.15

2. 60

3. An exception is thrown

4.30

Posted Date:-2022-01-02 12:07:25


Question:
hat will be the output of the following Python code?

class Demo:
    def __check(self):
        return " Demo's check "
    def display(self):
        print(self.check())
class Demo_Derived(Demo):
    def __check(self):
        return " Derived's check "
Demo().display()
Demo_Derived().display()

1.Demo’s check Derived’s check

2.Demo’s check Demo’s check

3.Derived’s check Demo’s check

4.Syntax error

Posted Date:-2022-01-02 12:08:34


Question:
How many except statements can a try-except block have?

1. zero

2.one

3.more than one

4.more than zero

Posted Date:-2022-01-02 12:16:47


Question:
Is the following Python code valid?

try:
    # Do something
except:
    # Do something
else:
    # Do something

1. no, there is no such thing as else

2.no, else cannot be used with except

3.no, else must come before except

4.yes

Posted Date:-2022-01-02 12:18:20


Question:
Is the following Python code valid?
try:
    # Do something
except:
    # Do something
finally:
    # Do something

1.no, there is no such thing as finally

2.no, finally cannot be used with except

3.no, finally must come before except

4.yes

Posted Date:-2022-01-02 12:17:56


Question:
Methods of a class that provide access to private members of the class are called as ______ and ______

1.getters/setters

2.__repr__/__str__

3.user-defined functions/in-built functions

4.__init__/__del__

Posted Date:-2022-01-02 12:11:57


Question:
What does built-in function help do in context of classes?

1. Determines the object name of any value

2.Determines the class identifiers of any value

3.Determines class description of any built-in type

4.Determines class description of any user-defined built-in type

Posted Date:-2022-01-02 11:58:44


Question:
What does built-in function type do in context of classes?

1.Determines the object name of any value

2.Determines the class name of any value

3.Determines class description of any value

4.Determines the file name of any value

Posted Date:-2022-01-02 11:57:18


Question:
What does single-level inheritance mean?

1.A subclass derives from a class which in turn derives from another class

2.A single superclass inherits from multiple subclasses

3. A single subclass derives from a single superclass

4.Multiple base classes inherit a single derived class

Posted Date:-2022-01-02 12:00:43


Question:
What happens when ‘1’ == 1 is executed?

1.we get a True

2.we get a False

3. an TypeError occurs

4.a ValueError occurs

Posted Date:-2022-01-02 12:21:42


Question:
What is the biggest reason for the use of polymorphism?

1. It allows the programmer to think at a more abstract level

2. There is less program code to write

3. The program will have a more elegant design and will be easier to maintain and update

4.Program code takes up less space

Posted Date:-2022-01-02 12:05:13


Question:
What is the use of duck typing?

1.More restriction on the type values that can be passed to a given method

2.No restriction on the type values that can be passed to a given method

3.Less restriction on the type values that can be passed to a given method

4.Makes the program code smaller

Posted Date:-2022-01-02 12:05:37


Question:
What type of inheritance is illustrated in the following Python code?

class A():
    pass
class B(A):
    pass
class C(B):
    pass

1.Multi-level inheritance

2.Multiple inheritance

3.Hierarchical inheritance

4.Single-level inheritance

Posted Date:-2022-01-02 12:00:16


Question:
What type of inheritance is illustrated in the following Python code?
class A():
    pass
class B():
    pass
class C(A,B):
    pass

1. Multi-level inheritance

2.Multiple inheritance

3.Hierarchical inheritance

4.Single-level inheritance

Posted Date:-2022-01-02 11:59:35


Question:
What will be the output of the following Python code?

 class student:
    def __init__(self):
        self.marks = 97
        self.__cgpa = 8.7
    def display(self):
        print(self.marks)
obj=student()
print(obj._student__cgpa)

1.The program runs fine and 8.7 is printed

2. Error because private class members can’t be accessed

3.Error because the proper syntax for name mangling hasn’t been implemented

4.he program runs fine but nothing is printed

Posted Date:-2022-01-02 12:14:16


Question:
What will be the output of the following Python code?

class A:
    def one(self):
        return self.two()
 
    def two(self):
        return 'A'
 
class B(A):
    def two(self):
        return 'B'
obj1=A()
obj2=B()
print(obj1.two(),obj2.two())

1. A A

2. A B

3.B B

4.An exception is thrown

Posted Date:-2022-01-02 11:59:03


Question:
What will be the output of the following Python code?

class A:
    def one(self):
        return self.two()    	
    def two(self):
        return 'A'   
class B(A):
    def two(self):
        return 'B'
obj2=B()
print(obj2.two())

1.A

2.An exception is thrown

3.A B

4.B

Posted Date:-2022-01-02 12:09:27


Question:
What will be the output of the following Python code?

class A:
    def __init__(self):
        self.multiply(15)
    def multiply(self, i):
        self.i = 4 * i;
class B(A):
    def __init__(self):
        super().__init__()
        print(self.i)
 
    def multiply(self, i):
        self.i = 2 * i;
obj = B()

1. 15

2.30

3.An exception is thrown

4.60

Posted Date:-2022-01-02 12:08:11


Question:
What will be the output of the following Python code?

class A:
    def __init__(self):
        self._x = 5       
class B(A):
    def display(self):
        print(self._x)
def main():
    obj = B()
    obj.display()
main()

1.Error, invalid syntax for object declaration

2.Nothing is printed

3.5

4.Error, private class member can’t be accessed in a subclass

Posted Date:-2022-01-02 12:03:21


Question:
What will be the output of the following Python code?

class A:
    def __init__(self):
        self.__x = 1
class B(A):
    def display(self):
        print(self.__x)
def main():
    obj = B()
    obj.display()
main()

1.1

2. 0

3.Error, invalid syntax for object declaration

4.Error, private class member can’t be accessed in a subclass

Posted Date:-2022-01-02 12:02:56


Question:
What will be the output of the following Python code?

class A:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __str__(self):
        return 1
    def __eq__(self, other):
        return self.x * self.y == other.x * other.y
obj1 = A(5, 2)
obj2 = A(2, 5)
print(obj1 == obj2)

1.False

2.1

3. True

4.An exception is thrown

Posted Date:-2022-01-02 12:08:59


Question:
What will be the output of the following Python code?

class A:
    def __init__(self,x):
        self.x = x
    def count(self,x):
        self.x = self.x+1
class B(A):
    def __init__(self, y=0):
        A.__init__(self, 3)
        self.y = y
    def count(self):
        self.y += 1     
def main():
    obj = B()
    obj.count()
    print(obj.x, obj.y)
main()

1.3 0

2.3 1

3.0 1

4.An exception in thrown

Posted Date:-2022-01-02 12:01:35


Question:
What will be the output of the following Python code?

class A:
    def __init__(self,x=3):
        self._x = x        
class B(A):
    def __init__(self):
        super().__init__(5)
    def display(self):
        print(self._x)
def main():
    obj = B()
    obj.display()
 
main()

1.5

2.Error, class member x has two values

3.3

4.Error, protected class member can’t be accessed in a subclass

Posted Date:-2022-01-02 12:03:49


Question:
What will be the output of the following Python code?

class A:
    def __repr__(self):
        return "1"
class B(A):
    def __repr__(self):
        return "2"
class C(B):
    def __repr__(self):
        return "3"
o1 = A()
o2 = B()
o3 = C()
print(obj1, obj2, obj3)

1. 1 1 1

2. 1 2 3

3.1’ ‘1’ ‘1’

4.An exception is thrown

Posted Date:-2022-01-02 12:06:58


Question:
What will be the output of the following Python code?

class A:
    def __str__(self):
        return '1'
class B(A):
    def __init__(self):
        super().__init__()
class C(B):
    def __init__(self):
        super().__init__()
def main():
    obj1 = B()
    obj2 = A()
    obj3 = C()
    print(obj1, obj2,obj3)
main()

1.1 1 1

2.1 2 3

3.‘1’ ‘1’ ‘1’

4. An exception is thrown

Posted Date:-2022-01-02 12:06:08


Question:
What will be the output of the following Python code?

class Demo:
     def __init__(self):
         self.a = 1
         self.__b = 1
 
     def get(self):
         return self.__b
 
obj = Demo()
print(obj.get())

1.The program has an error because there isn’t any function to return self.a

2.The program has an error because b is private and display(self) is returning a private member

3.The program has an error because b is private and hence can’t be printed

4. The program runs fine and 1 is printed

Posted Date:-2022-01-02 12:12:46


Question:
What will be the output of the following Python code?

class Demo:
    def check(self):
        return " Demo's check "  
    def display(self):
        print(self.check())
class Demo_Derived(Demo):
    def check(self):
        return " Derived's check "
Demo().display()
Demo_Derived().display()

1.Demo’s check Derived’s check

2.Demo’s check Demo’s check

3.Derived’s check Demo’s check

4.Syntax error

Posted Date:-2022-01-02 12:07:47


Question:
What will be the output of the following Python code?

class Demo:
    def __init__(self):
        self.a = 1
        self.__b = 1
 
    def display(self):
        return self.__b
 
obj = Demo()
print(obj.__b)

1. The program has an error because there isn’t any function to return self.a

2. The program has an error because b is private and display(self) is returning a private member

3. The program has an error because b is private and hence can’t be printed

4.The program runs fine and 1 is printed

Posted Date:-2022-01-02 12:11:36


Question:
What will be the output of the following Python code?

class Demo:
    def __init__(self):
        self.x = 1
    def change(self):
        self.x = 10
class Demo_derived(Demo):
    def change(self):
        self.x=self.x+1
        return self.x
def main():
    obj = Demo_derived()
    print(obj.change())
 
main()

1.11

2. 2

3.1

4.An exception is thrown

Posted Date:-2022-01-02 12:06:32


Question:
What will be the output of the following Python code?

class fruits:
    def __init__(self):
        self.price = 100
        self.__bags = 5
    def display(self):
        print(self.__bags)
obj=fruits()
obj.display()

1.The program has an error because display() is trying to print a private class member

2.The program runs fine but nothing is printed

3.The program runs fine and 5 is printed

4. The program has an error because display() can’t be accessed

Posted Date:-2022-01-02 12:13:48


Question:
What will be the output of the following Python code?

class objects:
    def __init__(self):
        self.colour = None
        self._shape = "Circle" 
 
    def display(self, s):
        self._shape = s
obj=objects()
print(obj._objects_shape)

1.The program runs fine because name mangling has been properly implemented

2.Error because the member shape is a protected member

3.Error because the proper syntax for name mangling hasn’t been implemented

4. Error because the member shape is a private member

Posted Date:-2022-01-02 12:15:59


Question:
What will be the output of the following Python code?

def foo():
    try:
        print(1)
    finally:
        print(2)
foo()

1. 1 2

2.1

3.2

4.none of the mentioned

Posted Date:-2022-01-02 12:20:46


Question:
What will be the output of the following Python code?

def foo():
    try:
        return 1
    finally:
        return 2
k = foo()
print(k)

1.1

2.2

3.3

4. error, there is more than one return statement in a single try-finally block

Posted Date:-2022-01-02 12:20:20


Question:
What will be the output of the following Python code?

try:
    if '1' != 1:
        raise "someError"
    else:
        print("someError has not occurred")
except "someError":
    print ("someError has occurred")

1.someError has occurred

2. someError has not occurred

3.invalid code

4.none of the mentioned

Posted Date:-2022-01-02 12:21:19


Question:
What will be the output of the following Python code?

x=10
y=8
assert x>y, 'X too small'

1.Assertion Error

2. 10 8

3. No output

4.108

Posted Date:-2022-01-02 12:22:22


Question:
When is the finally block executed?

1. when there is no exception

2. when there is an exception

3. only if some condition that has been specified is satisfied

4.always

Posted Date:-2022-01-02 12:19:57


Question:
When will the else part of try-except-else be executed?

1.always

2.when an exception occurs

3. when no exception occurs

4.when an exception occurs in to except block

Posted Date:-2022-01-02 12:17:09


Question:
Which of the following best describes polymorphism?

1. Ability of a class to derive members of another class as a part of its own definition

2. Means of bundling instance variables and methods in order to restrict access to certain class members

3.Focuses on variables and passing of variables to functions

4.Allows for objects of different types and behaviour to be treated as the same general type

Posted Date:-2022-01-02 12:04:46


Question:
Which of the following is false about protected class members?

1.They begin with one underscore

2.They can be accessed by subclasses

3.They can be accessed by name mangling method

4. They can be accessed within a class

Posted Date:-2022-01-02 12:15:14


Question:
Which of the following is not a type of inheritance?

1.Double-level

2.Multi-level

3.Single-level

4.Multiple

Posted Date:-2022-01-02 11:57:45


Question:
Which of the following is the most suitable definition for encapsulation?

1. Ability of a class to derive members of another class as a part of its own definition

2.Means of bundling instance variables and methods in order to restrict access to certain class members

3. Focuses on variables and passing of variables to functions

4.Allows for implementation of elegant software that is well designed and easily modified

Posted Date:-2022-01-02 12:11:04


Question:
Which of the following statements is true?

1.A non-private method in a superclass can be overridden

2.A subclass method can be overridden by the superclass

3. A private method in a superclass can be overridden

4.Overriding isn’t possible in Python

Posted Date:-2022-01-02 12:09:56


Question:
Which of the following statements isn’t true?

1.A non-private method in a superclass can be overridde

2.A derived class is a subset of superclass

3.The value of a private variable in the superclass can be changed in the subclass

4.When invoking the constructor from a subclass, the constructor of superclass is automatically invoked

Posted Date:-2022-01-02 12:01:12


Question:
Which of these is not a fundamental features of OOP?

1.Encapsulation

2.Inheritance

3. Instantiation

4.Polymorphism

Posted Date:-2022-01-02 12:10:37


More MCQS

  1. Python MCQS - Function
  2. Python MCQS - GUI in python
  3. Python MCQS - Operators
  4. Python MCQS - Data type in python
  5. Python MCQS - loops in python
  6. Python MCQS - Numpy
  7. Python MCQS - sqlite3
  8. Python MCQS - Library
  9. Python MCQS - Pandas
  10. Python MCQs
  11. Dictionary Python MCQ set 1
  12. Dictionary Python MCQ set 2
  13. MCQ For Python Fundamentals
  14. MCQ Introduction to Python Section 1
  15. MCQ Introduction to Python Section 2
  16. MCQ Introduction to Python Section 3
  17. MCQ on Flow of Control in Python Set 1
  18. MCQ on Flow of Control in Python Set 2
  19. MCQ on Python String Set 1
  20. File Handling in Python section 1
  21. File Handling in Python section 2
  22. Python Functions MCQS Set 1
  23. Python Functions MCQS Set 2
  24. MCQ on List in Python
  25. Pandas MCQ Questions Set 1
  26. Pandas MCQ Questions Set 2
  27. Tuple MCQ in Python
  28. Python dataframe MCQ
  29. Python Mcq Set 1
  30. Python Mcq Set 2
  31. Python Mcq Set 3
  32. Python Mcq Set 4
  33. Python Mcq Set 5
  34. Python Mcq Set 6
  35. Python Mcq Set 7
  36. Python Mcq Set 8
  37. Python Mcq Set 9
  38. Python Mcq Set 10
  39. Python Mcq Set 11
  40. Python Mcq Set 12
  41. Python Mcq Set 13
  42. Python Mcq Set 14
  43. Python Mcq Set 15
  44. Python Mcq Set 16
  45. Python Mcq Set 17
  46. Python Mcq Set 18
  47. Python Mcq Set 19
  48. Python Mcq Set 20
  49. Python Mcq Set 21
  50. Python MCQ
  51. Python MCQ Questions with Answer
  52. Test
  53. python mcq question and answer
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!